﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using DataIntegration;

namespace FileWatcherService
{
    partial class FileWatcherService : ServiceBase
    {
        SourceFileWatcher watcher1;
        SourceFileWatcher watcher2;
        SourceFileWatcher watcher3;
        List<SourceFileWatcher> watchlist1 = new List<SourceFileWatcher>();
        List<SourceFileWatcher> watchlist2 = new List<SourceFileWatcher>();
            
        public FileWatcherService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        protected override void OnStop()
        {
            //watcher1.Stop();
            //watcher2.Stop();
            watcher1 = null;
            watcher2 = null;
            watcher3 = null;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            try
            {
                string _toppath = ConfigurationManager.AppSettings["Path1"].ToString();
                DirectoryInfo di = new DirectoryInfo(_toppath);
                foreach (DirectoryInfo dir in di.GetDirectories())
                {
                    watchlist1.Add(new SourceFileWatcher(_toppath + dir.Name, DataSources.Path1));
                
                }
                for (int i=0; i < watchlist1.Count; i++)
                {
                    watchlist1[i].StartListening();
                }
                _toppath = ConfigurationManager.AppSettings["Path2"].ToString();
                 di = new DirectoryInfo(_toppath);
                 foreach (DirectoryInfo dir in di.GetDirectories())
                 {
                     watchlist2.Add(new SourceFileWatcher(_toppath + dir.Name, DataSources.Path2));

                 }
                 for (int i = 0; i < watchlist2.Count; i++)
                 {
                     watchlist2[i].StartListening();
                 }
                //watcher1 = new SourceFileWatcher(DataSources.Path1);
                //watcher2 = new SourceFileWatcher(DataSources.Path2);
                watcher3 = new SourceFileWatcher(DataSources.Path3);
                //watcher1.StartListening();
                //watcher2.StartListening();
                watcher3.StartListening();
                SourceFileWatcher.StartProcessing();
                //SourceFileWatcher watcher3 = new SourceFileWatcher(DataSources.Dhcp); 
                //SourceFileWatcher watcher1 = new SourceFileWatcher(DataSources.InternalProxy);
                //SourceFileWatcher watcher4 = new SourceFileWatcher(DataSources.ExternalSmtp);
                //SourceFileWatcher watcher5 = new SourceFileWatcher(DataSources.InternalSmtp);

            }
            catch (Exception ex)
            {
                //TODO:  log to event log
                //Console.WriteLine(ex.Message);
                //Console.WriteLine(ex.InnerException);
                //Console.WriteLine(ex.StackTrace);
            }
        }
    }
}
